﻿$(function () {

    var editBtn = $(".table .btn-edit");
    var faceUpload = $("#face_upload");

    editBtn.bind("click", function () {

        var popUp = $(".portlet");
        var thisTr = $(this).closest("tr");
        var thisForm = $(".portlet form[name='base']");

        //更新表单id
        popUp.find("form").attr("action", "#" + thisTr.attr("data-id"));

        $.ajax({
            type: "POST",
            url: "/Services/AUserService.asmx/GetUserInfo",
            data: "{uid:'" + thisTr.attr("data-id") + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (callback) {
                var msg = callback.d;
                var
                    realName = thisForm.find("input[name='realname']"),
                    tel = thisForm.find("input[name='phone']"),
                    QQ = thisForm.find("input[name='QQ']"),
                    groupList = thisForm.find("select[name='grouplist']"),
                    email = thisForm.find("input[name='Email']");

                if (msg) {

                    realName.val(msg.RealName);
                    tel.val(msg.Telephone);
                    QQ.val(msg.QQ);
                    email.val(msg.Email);
                    
                    groupList.children().each(function () {
                        
                        var _this = $(this);

                        if (_this.val() == msg.GID) {
                            _this.attr("selected", "selected");
                        } else {
                            _this.removeAttr("selected");
                        }

                    });

                } else {
                    realName.val("");
                    tel.val("");
                    QQ.val("");
                    email.val("");
                    groupList.children().removeAttr("selected");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                
            }
        });

    });

    faceUpload.bind("change", function () {
        alert("信息已更改");
    });

    $(".portlet form[name='base']").validate({
        rules: {
            realname: {
                required: true
            },
            phone: {
                required: true
            },
            QQ: {
                required: false
            },
            Email: {
                required: false,
                email:true
            }
        },

        highlight: function (element) { // hightlight error inputs
            $(element).closest('.control-group').addClass('error'); 
        },

        success: function (label, element) {
            $(element).closest('.control-group').removeClass('error');
        },

        errorPlacement: function (error, element) {
            //error.remove();
        }
    });
});